/* VertexPair*/

package graph;

/**
 * The VertexPair represents a pair of objects that act as vertices in a
 * WUGraph (weighted, undirected graph).  The purpose of a VertexPair is to
 * act as a key in Java's built-in Hashtable class.  The hashCode() and
 * equals() functions, which are called by Java's hash tables, are designed
 * so that the order of the two objects is immaterial; (u, v) is the same as
 * (v, u).
 */

class VertexPair {
	protected Object object1;
	protected Object object2;

	protected VertexPair(Object o1, Object o2) {
		object1 = o1;
		object2 = o2;
	}

	/**
	 * otherVertex() returns the other vertex in "this" VertexPair from the input vertex.
	 * If vertex is not in "this" return null.
	 * 
	 * @param vertex is the vertex to be compared to find its pair.
	 * @return is the input vertex pair.
	 */
	public Object otherVertex(Object vertex) {
		if (vertex == object1) {
			return object2;
		} else if (vertex == object2) {
			return object1;
		} else {
			return null;
		}
	}
	/**
	 * hashCode() returns a hashCode equal to the sum of the hashCodes of each
	 * of the two objects of the pair, so that the order of the objects will
	 * not affect the hashCode.  Self-edges are treated differently:  we don't
	 * add an object's hashCode to itself, since the result would always be even.
	 * We add one to the hashCode so that a self-edge will not collide with the
	 * object itself if vertices and edges are stored in the same hash table.
	 */
	public int hashCode() {
		if (object1.equals(object2)) {
			return object1.hashCode() + 1;
		} else {
			return object1.hashCode() + object2.hashCode();
		}
	}

	/**
	 * equals() returns true if this VertexPair represents the same unordered
	 * pair of objects as the parameter "o".  The order of the pair does not
	 * affect the equality test, so (u, v) is found to be equal to (v, u).
	 */
	public boolean equals(Object o) {
		if (o instanceof VertexPair) {
			return ((object1.equals(((VertexPair) o).object1)) &&
					(object2.equals(((VertexPair) o).object2))) ||
					((object1.equals(((VertexPair) o).object2)) &&
							(object2.equals(((VertexPair) o).object1)));
		} else {
			return false;
		}
	}
	/**
	 * toString() converts the VertexPair object to a string 
	 * represented as ( object1.toString(), object2.toString() )
	 * 
	 * @return a string representation of "this" VertexPair
	 */
	public String toString() {
		return ("( " + object1 +", " + object2 +" )");
	}
}
